package com.fsnbigdata.repository;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Ecosystem;
import com.fsnbigdata.domain.QEcosystem;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = EcosystemRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = EcosystemRepositoryCustom.class)
@Transactional(readOnly = true)
public class EcosystemRepositoryImpl extends QueryDslRepositorySupportExt<Ecosystem> implements EcosystemRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    EcosystemRepositoryImpl() {
        super(Ecosystem.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ADMINISTRATIVE_DEPARTMENT = "administrativeDepartment";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM_RESOURCES_FOREST = "organismResourcesForest";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String GEOLOGY_LITHOLOGY = "geologyLithology";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String MAINTAINED = "maintained";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String TYPE = "type";


    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM_RESOURCES_PLANT = "organismResourcesPlant";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String GEOLOGY_LANDFORM = "geologyLandform";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM_RESOURCES_VEGE_TYPE = "organismResourcesVegeType";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CURRENT_LEVEL = "currentLevel";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String APPROVER = "approver";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM_RESOURCES_ANIMAL = "organismResourcesAnimal";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATE_DATE = "createDate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String AREA = "area";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CATEGORY = "category";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ADMINISTRATIVE_LOCATION = "administrativeLocation";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String SOIL = "soil";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CURRENT_LEVEL_APPROVE_DATE = "currentLevelApproveDate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String TOURISM_RESOURCES = "tourismResources";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String COORDINATE = "coordinate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ADMINISTRATIVE_REGION = "administrativeRegion";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String GEOLOGY_STRUCTURE = "geologyStructure";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String GEOLOGY_HYDROGEOLOGY = "geologyHydrogeology";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CLIMATE = "climate";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String SOCIAL_ECONOMY = "socialEconomy";

    @Override
    public Page<Ecosystem> findAll(Predicate predicate, Pageable pageable) {
        QEcosystem ecosystem = QEcosystem.ecosystem;
        JPQLQuery<Ecosystem> query = from(ecosystem).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        applyPagination(pageable, query);
        return loadPage(query, pageable, ecosystem);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Ecosystem> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QEcosystem ecosystem = QEcosystem.ecosystem;
        JPQLQuery<Ecosystem> query = from(ecosystem);
        if(creator != null ){
            query.where(ecosystem.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { ecosystem.name, ecosystem.administrativeLocation, ecosystem.area, ecosystem.maintained, ecosystem.category, ecosystem.currentLevel, ecosystem.administrativeDepartment, ecosystem.createDate, ecosystem.approver, ecosystem.currentLevelApproveDate, ecosystem.coordinate, ecosystem.administrativeRegion, ecosystem.geologyStructure, ecosystem.geologyLithology, ecosystem.geologyLandform, ecosystem.geologyHydrogeology, ecosystem.soil, ecosystem.climate, ecosystem.organismResourcesForest, ecosystem.organismResourcesVegeType, ecosystem.organismResourcesPlant, ecosystem.organismResourcesAnimal, ecosystem.tourismResources, ecosystem.socialEconomy, ecosystem.type };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, ecosystem.name).map(ADMINISTRATIVE_LOCATION, ecosystem.administrativeLocation).map(AREA, ecosystem.area).map(MAINTAINED, ecosystem.maintained).map(CATEGORY, ecosystem.category).map(CURRENT_LEVEL, ecosystem.currentLevel).map(ADMINISTRATIVE_DEPARTMENT, ecosystem.administrativeDepartment).map(CREATE_DATE, ecosystem.createDate).map(APPROVER, ecosystem.approver).map(CURRENT_LEVEL_APPROVE_DATE, ecosystem.currentLevelApproveDate).map(COORDINATE, ecosystem.coordinate).map(ADMINISTRATIVE_REGION, ecosystem.administrativeRegion).map(GEOLOGY_STRUCTURE, ecosystem.geologyStructure).map(GEOLOGY_LITHOLOGY, ecosystem.geologyLithology).map(GEOLOGY_LANDFORM, ecosystem.geologyLandform).map(GEOLOGY_HYDROGEOLOGY, ecosystem.geologyHydrogeology).map(SOIL, ecosystem.soil).map(CLIMATE, ecosystem.climate).map(ORGANISM_RESOURCES_FOREST, ecosystem.organismResourcesForest).map(ORGANISM_RESOURCES_VEGE_TYPE, ecosystem.organismResourcesVegeType).map(ORGANISM_RESOURCES_PLANT, ecosystem.organismResourcesPlant).map(ORGANISM_RESOURCES_ANIMAL, ecosystem.organismResourcesAnimal).map(TOURISM_RESOURCES, ecosystem.tourismResources).map(SOCIAL_ECONOMY, ecosystem.socialEconomy).map( TYPE, ecosystem.type );
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, ecosystem);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Ecosystem> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QEcosystem ecosystem = QEcosystem.ecosystem;
        JPQLQuery<Ecosystem> query = from(ecosystem);
        Path<?>[] paths = new Path<?>[] { ecosystem.name, ecosystem.administrativeLocation, ecosystem.area, ecosystem.maintained, ecosystem.category, ecosystem.currentLevel, ecosystem.administrativeDepartment, ecosystem.createDate, ecosystem.approver, ecosystem.currentLevelApproveDate, ecosystem.coordinate, ecosystem.administrativeRegion, ecosystem.geologyStructure, ecosystem.geologyLithology, ecosystem.geologyLandform, ecosystem.geologyHydrogeology, ecosystem.soil, ecosystem.climate, ecosystem.organismResourcesForest, ecosystem.organismResourcesVegeType, ecosystem.organismResourcesPlant, ecosystem.organismResourcesAnimal, ecosystem.tourismResources, ecosystem.socialEconomy, ecosystem.type };
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(ecosystem.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, ecosystem.name).map(ADMINISTRATIVE_LOCATION, ecosystem.administrativeLocation).map(AREA, ecosystem.area).map(MAINTAINED, ecosystem.maintained).map(CATEGORY, ecosystem.category).map(CURRENT_LEVEL, ecosystem.currentLevel).map(ADMINISTRATIVE_DEPARTMENT, ecosystem.administrativeDepartment).map(CREATE_DATE, ecosystem.createDate).map(APPROVER, ecosystem.approver).map(CURRENT_LEVEL_APPROVE_DATE, ecosystem.currentLevelApproveDate).map(COORDINATE, ecosystem.coordinate).map(ADMINISTRATIVE_REGION, ecosystem.administrativeRegion).map(GEOLOGY_STRUCTURE, ecosystem.geologyStructure).map(GEOLOGY_LITHOLOGY, ecosystem.geologyLithology).map(GEOLOGY_LANDFORM, ecosystem.geologyLandform).map(GEOLOGY_HYDROGEOLOGY, ecosystem.geologyHydrogeology).map(SOIL, ecosystem.soil).map(CLIMATE, ecosystem.climate).map(ORGANISM_RESOURCES_FOREST, ecosystem.organismResourcesForest).map(ORGANISM_RESOURCES_VEGE_TYPE, ecosystem.organismResourcesVegeType).map(ORGANISM_RESOURCES_PLANT, ecosystem.organismResourcesPlant).map(ORGANISM_RESOURCES_ANIMAL, ecosystem.organismResourcesAnimal).map(TOURISM_RESOURCES, ecosystem.tourismResources).map(SOCIAL_ECONOMY, ecosystem.socialEconomy).map( TYPE,ecosystem.type );
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, ecosystem);
    }
}
